package com.youkeyi.ddy.cloud.gateway.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * YoukeyiDdyDemoFilter
 *
 * @Author liuxiawang
 * @Date 2024/6/29 11:31 AM
 * @Location shenzhen.china
 */
@Slf4j
@Component
public class YoukeyiDdyDemoFilter implements GlobalFilter, Ordered {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public int getOrder() {
        return -1; // 设置优先级，数值越小优先级越高
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 返回403等
        boolean valid = validateRequest(exchange);
        if (valid) {
            exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
            exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);

            ResponseContent response = new ResponseContent(false, 403, "暂无权限", "");
            byte[] bytes;
            try {
                bytes = objectMapper.writeValueAsBytes(response);
            } catch (Exception e) {
                log.error("Error serializing response body", e);
                return Mono.error(e);
            }

            // 校验不通过，返回403等
            return exchange.getResponse().writeWith(Mono.just(exchange.getResponse()
                    .bufferFactory()
                    .wrap(bytes)));
        }

        // 校验通过
        return chain.filter(exchange);
    }

    private boolean validateRequest(ServerWebExchange exchange) {
        // 在这里添加你的校验逻辑，例如检查请求头或参数
        // 获取请求
        ServerHttpRequest request = exchange.getRequest();

        // 这里假设总是返回 false 表示校验不通过
        return false;
    }

    public class ResponseContent {

        public ResponseContent(boolean success, Integer code, String message, String key) {
            this.success = success;
            this.code = code;
            this.message = message;
            this.key = key;
        }

        private final Integer SUCCESS_CODE = 10000;
        private final Integer ERROR_CODE = 0;

        /**
         * 执行是否成功
         */
        private boolean success;
        /**
         * 执行结果码
         */
        private Integer code;
        /**
         * 响应携带消息
         */
        private String message;
        /**
         * 响应携带业务主键
         */
        private String key;

        public void setSuccessMessage(String message) {
            this.success = Boolean.TRUE;
            this.message = message;
            this.code = SUCCESS_CODE;
        }


        public void setErrorMessage(String message) {
            this.success = Boolean.FALSE;
            this.message = message;
            this.code = ERROR_CODE;
        }

        public boolean isSuccess() {
            return success;
        }

        public void setSuccess(boolean success) {
            this.success = success;
        }

        public Integer getCode() {
            return code;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }
    }
}
